
package msu.ml.data.level2;

import java.io.*;
import java.util.*;

/**
 * Service used to retrieve SiteInfo objects
 * for desired sites. Uses the Singleton design
 * pattern.
 *
 * @author Reginald M Mead
 * @version 1.0
 */
public class SiteInfoService
{
	private static SiteInfoService instance = null;
	private SiteInfo[] sites;

	private SiteInfoService()
	{
		try
		{
			ArrayList<SiteInfo> siteList = 
				new ArrayList<SiteInfo>();

			BufferedReader br = new BufferedReader(
					new FileReader("LevelIIDataLoader"+File.separatorChar+
						"Locations.dat"));

			String temp = br.readLine();
			while(temp != null)
			{
				siteList.add(new SiteInfo(temp));
				temp = br.readLine();
			}

			this.sites = siteList.toArray(new SiteInfo[0]);

			br.close();
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
	}


	/**
	 * Get an instance of a SiteInfoService
	 *
	 * @return a SiteInfoService instance
	 */
	public static SiteInfoService getInstance()
	{
		if(instance == null)
			instance = new SiteInfoService();

		return instance;
	}

	/**
	 * The number of sites for which this SiteInfoService
	 * has information.
	 *
	 * @return the number of available sites
	 */
	public int size()
	{
		return this.sites.length;
	}

	/**
	 * Retrieve the ith SiteInfo
	 *
	 * @return the requested SiteInfo object
	 */
	public SiteInfo get(int i)
	{
		return sites[i];
	}

	/**
	 * Get the SiteInfo instance for the site
	 * with the specified ID.
	 *
	 * @param id site id of desired SiteInfo
	 * @return a SiteInfo instance
	 */
	public SiteInfo getInfoById(int id)
	{
		for(SiteInfo info : this.sites)
			if(info.getId() == id)
				return info;
		return null;
	}

	/**
	 * Get the SiteInfo instance for the site
	 * with the specified call sign.
	 *
	 * @param call call sign of desired SiteInfo
	 * @return a SiteInfo instance
	 */
	public SiteInfo getInfoByCall(String call)
	{
		for(SiteInfo info : this.sites)
			if(info.getCall().equals(call))
				return info;
		return null;
	}

}


	
